﻿/**
 * Created by zhuchunfeng on 2017/7/14.
 */
$(function() {
	fix.divDisplay(!true);
	zy.searchDD();
	fix.PhoneBrand();
	fix.step();
	fix.formClear();
	fix.lastState();
	reclaim.phone();
	fix.orderConfirm();

	reclaim.reclaimData();
});
 var $eventUrl = "http://www.xiaoerkx.com/actity/";
var zy = {
	headerNav: function() {
		$("#navphone > li").each(function() {
			$(this).click(function() {
				$("#navphone > li").removeClass("active");
				$(this).addClass("active");
			});
		});
	},
	searchDD: function() {
			$(".searchtype > dd").bind("mouseover mouseout", function() {
				$(this).toggleClass('current');
			});
		} // serach page
};

var displayBolck = 'display-block';
var displayNone = 'display-none';
var step5Div = "#step5";
var step6Div = "#step6";
var step1 = '#step1 ul li';
var step2Div = '#step2';
var step2 = step2Div + ' ul li';
var step3Div = '#step3';
var step3 = step3Div + ' ul li';
var step4Div = '#step4';
var step4 = step4Div + ' ul li';
var active = 'selected';

//故障维修
var fix = {
	step: function() {
		fix.step1();
	},
	step1: function() {
		$(step1).on('click', function() {
			$(step1).removeClass(active)
			$(this).addClass(active);
			//
			fix.PhoneModel($(this).attr('brandid'));
			fix.step2();
			$(step2Div).removeClass(displayNone).addClass(displayBolck)
				//清空存在的数据
			$('#step3,#step4,#step5,#step6').removeClass(displayBolck).addClass(displayNone);
			fix.orderClear();
			fix.GD($('#step2').offset().top - 200);
			//100
		});
	},
	step2: function() {
		$(step2).on('click', function() {
			$(step2).removeClass(active)
			$(this).addClass(active);
			fix.ModelColors($(this).attr('modelid'));
			fix.step3();
			$(step3Div).removeClass(displayNone).addClass(displayBolck)
			$('#step4,#step5,#step6').removeClass(displayBolck).addClass(displayNone);
			fix.orderClear();
			fix.GD($('#step3').offset().top - 300);
			//400
		});
	},
	step3: function() {
		$(step3).on('click', function() {
			$(step3).removeClass(active)
			$(this).addClass(active);
			fix.Question($("#step2 ul li").filter('.selected').attr('modelid'));
			fix.step4();
			$(step4Div).removeClass(displayNone).addClass(displayBolck)
			fix.GD($('#step4').offset().top - 400);
			//700
		});
	},
	step4: function() {
		$('#step4 .Q_one > li').mouseover(function() {
			$(this).find('article').removeClass(displayNone).addClass(displayBolck)
		});

		$('#step4 .Q_one > li').mouseout(function() {
			$(this).find('article').removeClass(displayBolck).addClass(displayNone);
		});
		//step 子
		$('#step4 .Q_two > li').on('click', function() {
			var phonedata = $(this).attr('phonedata');
			var dec = $(this).text();
			var price = $(this).attr('price');
			if ($(this).hasClass(active)) {
				$(this).removeClass(active);
				fix.orderRemove(phonedata);
			} else {
				$(this).addClass(active)
					//
				fix.orderAdd(phonedata, dec, price);
			}
			//当子一个都没有选，父的处理
			if ($(this).parent().find('li').hasClass(active)) {
				$(this).parents('li').addClass(active);
			} else {
				$(this).parents('li').removeClass(active);
			}
			//5	
			fix.lastState();
			fix.progress2();
		});
	},
	lastState: function() {
		var count = (new Order()).count;
		if (count > 0) {
			$(step5Div).removeClass(displayNone).addClass(displayBolck);
			$(step6Div).removeClass(displayNone).addClass(displayBolck);
			fix.orderPrice();
			fix.orderHeight();
			fix.orderDel(); //zcsj
			fix.progress(2);
			fix.GD($('#step4').offset().top);
			//1000
		} else {
			$(step5Div).removeClass(displayBolck).addClass(displayNone);
			$(step6Div).removeClass(displayBolck).addClass(displayNone);
		}
	}, //
	orderClear: function() {
		$('#orderlist tr').remove();
	},
	orderPrice: function() {
		var price = 0;
		$('#orderlist .price').each(function(i) {
			price += parseFloat($(this).attr('price'));
		});
		$("#orderPrice").text('￥' + price);
		return price;
	}, //价格
	orderHeight: function() {
		var height = 162;
		var td = 66;
		var count = (new Order()).count;
		if (count >= 0) {
			height += td * count;
			$('#step5').height(height);
			$('#step5 .steps5_bglay').height(height);
		}
	}, //高度
	orderAdd: function(phoneData, dec, price) {
		// var pp, xh, ys, wt, subwt;
		// var arry = phoneData.split(',');
		// pp = arry[0];
		// xh = arry[1];
		// ys = arry[2];
		// wt = arry[3];
		// subwt = arry[4];
		var tr = '<tr phonedata=' + phoneData + '>' +
			'<td>' +
			'<ol>' +
			'<li>' + dec + '<del></del></li>' +
			'</ol>' +
			'</td>' +
			'<td>' + dec + '</td>' +
			'<td  class="price" price=' + price + '>￥' + price + '</td></tr>';
		$('#orderlist').append(tr);
	}, //添加维修项
	orderRemove: function(phoneData) {
		var ele = $("#orderlist tr[phonedata='" + phoneData + "']");
		var bool = ele.length > 0 ? true : false;
		if (bool) {
			ele.remove();
		}
	}, //删除
	orderDel: function() {
		$('del').on('click', function() {
			var phonedata = '[phonedata=' + $(this).parents('tr').attr('phonedata') + ']';
			var sub = $("#step4 .Q_two > li" + "[phonedata='" + $(this).parents('tr').attr('phonedata') + "']");
			sub.removeClass('selected');
			var mub = sub.parents('li');
			if (!mub.find('li').hasClass('selected')) {
				mub.removeClass('selected');
			}
			$(this).parents('tr').remove();
			fix.lastState();
			fix.progress2();
		});
	},
	divDisplay: function(isOpen) {
		if (isOpen) {
			$('#step2,#step3,#step4,#step5,#step6').removeClass(displayNone).addClass(displayBolck);

		} else {
			$('#step2,#step3,#step4,#step5,#step6').addClass(displayNone);
		}
		fix.orderHeight();
		fix.orderPrice();
	},
	progress: function(index) {
		var pro = $('.steps_bar li');

		if (index >= pro.length)
			index = pro.length - 1;
		if (index > 0 && index <= 4) {
			pro.removeClass('current').removeClass('done');
			for (var i = 0; i <= index; i++) {
				if (i == index) {
					$(pro[i]).addClass('current');
					return;
				}
				$(pro[i]).removeClass('current').addClass('done');
			}
		}

	}, //进度
	progress2: function() {
		var count = (new Order()).count;
		if (count <= 0) {
			fix.progress(1);
		}
	},
	orderConfirm: function() {
		$('.next-page1').on('click', function() {
			//验证 
			var valid = fix.formValid();
			if (!valid) {
				return valid;
			}
			layer.open({
				content: '您确认提交订单吗？',
				btn: ['确认', '取消'],
				shadeClose: false,
				yes: function() {
					//
					var jsonObj = [];
					var BrandNo = $("#step1 ul li").filter('.selected').attr('brandid');
					var PhoneModelNo = $("#step2 ul li").filter('.selected').attr('modelid');
					var nameID = '#username';
					var phoneID = '#phone';
					var addressID = '#address';
					var name = $(nameID).val();
					var phone = $(phoneID).val();
					var address = $(addressID).val();
					var memo = $('#order_message').val();
					var colorname = $("#step3 ul li").filter('.selected').text();
					var colorNo = $("#step3 ul li").filter('.selected').attr('colorid');
					var totalPrice = fix.orderPrice();
					$('#orderlist tr').each(function(i) {
						debugger
						var phonedata = $(this).attr('phonedata').split(',');
						var price = $(this).find('.price').attr('price');
						var jmodel = {
							"OrderType": 1,
							"BrandNo": BrandNo,
							"PhoneModelNo": PhoneModelNo,
							"FaultPrice": totalPrice,
							"phoneNo": phone,
							"UserName": name,
							"Memo": memo,
							"dtlFaultTypeNo": phonedata[1],
							"dtlFaultPrice": price,
							"Addresses": address,
							"ColorName": colorname,
							"ColorNo": colorNo
						};
						jsonObj.push(jmodel);
					});
					var json = JSON.stringify(jsonObj);

					$.ajax({
						type: "post",
						contentType: 'application/json; charset=UTF-8',
						url: $eventUrl + "Order",
						data: json,
						dataType: 'jsonp',
						jsonp: "jsonpcallback",
						async: false,
						success: function(data) {
							var code = data.Code;
							if (code == 0) {
								window.location.href = 'success.html';
							} else {
								layer.msg(data.Message);
							}
						}
					});
				},
				no: function() {
					return false;
				}
			});

		});
	},
	formValid: function() {
		var count = (new Order()).count;

		if (count <= 0) {
			layer.msg('请选择维修方案');
			return false;
		}


		var nameID = '#username';
		var phoneID = '#phone';
		var addressID = '#address';

		var name = $(nameID).val();
		var phone = $(phoneID).val();
		var address = $(addressID).val();
		if (name === undefined || name === "") {
			msg.tips('用户名不能为空！', nameID);
			return false;
		}
		var name1 = diy.IsLegalXmlString(name);
		var name2 = diy.nohtml(name);
		var name3 = diy.str(name);
		if (!name1 || name2 || !name3) {
			msg.tips('用户名不合法！', nameID);
			return false;
		}
		if (phone === undefined || phone === "") {
			msg.tips('手机号不能为空！', phoneID);

			return false;
		}
		var isPhone = diy.phone(phone);
		if (!isPhone) {
			msg.tips('请输入正确的手机号！', phoneID);

			return false;
		}
		if (address === undefined || address === "") {
			msg.tips('详情地址不能为空！', addressID);

			return false;
		}
		var address1 = diy.IsLegalXmlString(address);
		var address2 = diy.nohtml(address);
		// var address3 = diy.str(address);
		if (!address1 || address2) {
			msg.tips('详情地址不合法！', addressID);
			return false;
		}
		fix.progress(3);
		return true;
	},
	formClear: function() {
		var nameID = '#username';
		var phoneID = '#phone';
		var addressID = '#address';

		$(nameID).val('');
		$(phoneID).val('');
		$(addressID).val('');
		$('#order_message').val('');
	},
	GD: function(value) {
		$('html, body').stop().show().animate({
			scrollTop: value
		}, 1000);
	},
	PhoneBrand: function() {
		$.ajax({
			type: "post",
			contentType: 'application/json; charset=UTF-8',
			url: $eventUrl + "PhoneBrand",
			dataType: 'jsonp',
			jsonp: "jsonpcallback",
			async: false,
			success: function(data) {
				var code = data.Code;
				if (code == 0) {
					var $htmls = "";
					$(data.Data).each(function(j) {
						$htmls += "<li brandid='" + data.Data[j].BrandNo + "'>" + data.Data[j].BrandName + "</li>";
					});
					$('#step1 #brand_ul').html($htmls);
				}
			}
		});
	},
	PhoneModel: function(mid) {
		$.ajax({
			type: "get",
			contentType: 'application/json; charset=UTF-8',
			url: $eventUrl + "PhoneModel",
			data: {
				"id": mid
			},
			dataType: 'jsonp',
			jsonp: "jsonpcallback",
			async: false,
			success: function(data) {
				var code = data.Code;
				if (code == 0) {
					var $htmls = "";
					$(data.Data).each(function(j) {
						$htmls += "<li modelid='" + data.Data[j].PhoneModelNo + "'>" + data.Data[j].PhoneModelName + "</li>";
					});
					$('#step2 #model_ul').html($htmls);
				}
			}
		});
	},
	ModelColors: function(id) {
		$.ajax({
			type: "get",
			contentType: 'application/json; charset=UTF-8',
			url: $eventUrl + "ModelColors",
			data: {
				"id": id
			},
			dataType: 'jsonp',
			jsonp: "jsonpcallback",
			async: false,
			success: function(data) {
				var code = data.Code;
				if (code == 0) {
					var $htmls = "";
					$(data.Data).each(function(j) {
						$htmls += "<li colorid='" + data.Data[j].ColorNo + "'>" + data.Data[j].ColorName + "</li>";
					});
					$('#step3 #color_ul').html($htmls);
				}
			}
		});
	},
	Question: function(id) {
		$.ajax({
			type: "get",
			contentType: 'application/json; charset=UTF-8',
			url: $eventUrl + "Question",
			data: {
				"id": id
			},
			dataType: 'jsonp',
			jsonp: "jsonpcallback",
			async: false,
			success: function(data) {
				var code = data.Code;
				if (code == 0) {
					var $htmls = "";
					$(data.Data).each(function(j) {
						var sub = data.Data[j].Sub;
						// var pos = -165 * (j % 6) + 'px';
						var pos = -207 * (j % 5) + 'px';
						$htmls += "<li No='" + data.Data[j].No + "'>" + data.Data[j].Name + "<article class='display-none' style='left:" + pos + "'> <ul class='Q_two'>";
						for (var i = sub.length - 1; i >= 0; i--) {
							$htmls += "<li phonedata='" + fix.PJ(data.Data[j].No, sub[i].No) + "' price='" + sub[i].Price + "'>" + sub[i].Name + "</li>";
						}
						$htmls += "</ul></article></li>";
					});
					$('#step4 .Q_one').html($htmls);
				}
			}
		});
	},
	PJ: function(ag1, ag2) {
		return ag1 + "," + ag2;
	}

};
//
var reclaim = {
	phone: function() {
		var al = '#brand-contents li a';
		var active1 = 'mid';
		$(al).on('click', function() {
			$(al).removeClass(active1)
			$(this).addClass(active1);
		});
	},
	phoneData: function() {
		var data = [];
		// var model1={
		// 	phone:'iPhone4',
		// 	price:'500',
		// 	img:'resource/img/phone/p1.jpg'
		// }
		// var model2={
		// 	phone:'iPhone4s',
		// 	price:'500',
		// 	img:'resource/img/phone/p2.jpg'
		// }
		// var model3={
		// 	phone:'iPhone5',
		// 	price:'500',
		// 	img:'resource/img/phone/p3.jpg'
		// }
		var model4={
			phone:'iPhone5s',
			price:'400',
			img:'resource/img/phone/p4.jpg'
		}
		var model516={
			phone:'iPhone6(16g)',
			price:'1000',
			img:'resource/img/phone/p5.jpg'
		}
		var model564={
			phone:'iPhone6(64g)',
			price:'1300',
			img:'resource/img/phone/p5.jpg'
		}
		var model5128={
			phone:'iPhone6(128g)',
			price:'1600',
			img:'resource/img/phone/p5.jpg'
		}
		var model616={
			phone:'iPhone6p(16g)',
			price:'1200',
			img:'resource/img/phone/p6.jpg'
		}
	    var model664={
			phone:'iPhone6p(64g)',
			price:'1550',
			img:'resource/img/phone/p6.jpg'
		}
		var model6128={
			phone:'iPhone6p(128g)',
			price:'1900',
			img:'resource/img/phone/p6.jpg'
		}
		var model716={
			phone:'iPhone6s(16g)',
			price:'1300',
			img:'resource/img/phone/p7.jpg'
		}
		var model764={
			phone:'iPhone6s(64g)',
			price:'1700',
			img:'resource/img/phone/p7.jpg'
		}
		var model7128={
			phone:'iPhone6s(128g)',
			price:'2000',
			img:'resource/img/phone/p7.jpg'
		}
		var model816={
			phone:'iPhone6sp(16g)',
			price:'1400',
			img:'resource/img/phone/p8.jpg'
		}
		var model864={
			phone:'iPhone6sp(64g)',
			price:'1850',
			img:'resource/img/phone/p8.jpg'
		}
		var model8128={
			phone:'iPhone6sp(128g)',
			price:'2300',
			img:'resource/img/phone/p8.jpg'
		}
		var model932={
			phone:'iPhone7(32g)',
			price:'2600',
			img:'resource/img/phone/p9.jpg'
		}
		var model9128={
			phone:'iPhone7(128g)',
			price:'3400',
			img:'resource/img/phone/p9.jpg'
		}
		var model9256={
			phone:'iPhone7(256g)',
			price:'4400',
			img:'resource/img/phone/p9.jpg'
		}
		var model1032={
			phone:'iPhone7p(32g)',
			price:'3100',
			img:'resource/img/phone/p10.jpg'
		}
		var model10128={
			phone:'iPhone7p(128g)',
			price:'3900',
			img:'resource/img/phone/p10.jpg'
		}
		var model10256={
			phone:'iPhone7p(256g)',
			price:'4900',
			img:'resource/img/phone/p10.jpg'
		}

        // data.push(model1);
        // data.push(model2);
        // data.push(model3);
        data.push(model4);
        data.push(model516);
        data.push(model564);
        data.push(model5128);

        data.push(model616);
        data.push(model664);
        data.push(model6128);

        data.push(model716);
        data.push(model764);
        data.push(model7128);

        data.push(model816);
        data.push(model864);
        data.push(model8128);
    
        data.push(model932);
        data.push(model9128);
        data.push(model9256);

        data.push(model1032);
        data.push(model10128);
        data.push(model10256);  

		return data;
	},
	reclaimData:function(){
		var $html="";
		var data=reclaim.phoneData();
		for(var i=0;i<data.length;i++){
		    //$('.lists-content').remove();           
		   $html+="<li class='col-2 col-lg-3 col-md-3 lists-content-lists'>"
		   +"<a href='javascript:void(0)'>"
		   +"<img alt='"+data[i].phone+"' class='lists-content-img' style='width:121px;height:240px;' src='"+data[i].img+"'></a>"
		   +"<p class='item-name'>"+data[i].phone+"</p>"
		   +"<p>最高回收价：<span class='lists-price'>"+data[i].price+"</span></p></li>";
		}
		$('.lists-content').html($html);
        //
		$('.lists-content').find('a').on('click',function(){
             window.location.href = 'contact.html';
		});

	}


};

//
var serach = {
	serach: function(value) {
		if (value === "") {
			layer.msg("请填写手机号");
			return;
		}

		$.ajax({
			type: "get",
			contentType: 'application/json; charset=UTF-8',
			url: $eventUrl + "SearchByPhone",
			data: {
				"phone": value
			},
			dataType: 'jsonp',
			jsonp: "jsonpcallback",
			async: false,
			success: function(data) {

				var table = $("#search_order tbody");
				$("#search_order tbody tr").remove();
				var code = data.Code;
				var $htmls = "";
				if (code == 0) {
					$(data.Data).each(function(j) {
						$htmls += "<tr>";
						$htmls += "<td>" + data.Data[j].BrandName + "</td>" + "<td>" + data.Data[j].PhoneModelName + "</td>" + "<td>" + data.Data[j].ColorName + "</td>" + "<td>" + data.Data[j].UserName + "</td>" + "<td>" + data.Data[j].phoneNo + "</td>" + "<td>" + data.Data[j].FaultTypeNoName + "</td>" + "<td>" + data.Data[j].dtlFaultPrice + "</td>" + "<td>" + data.Data[j].Addresses + "</td>" + "<td>" + data.Data[j].Memo + "</td>";
						$htmls += "</tr>";
					});

				} else {
					$htmls += "<tr>";
					$htmls += "<td colspan='5'>未查询到记录</td>"
					$htmls += "</tr>";
				}
				table.html($htmls);
			}
		});
	}
}


//DIY Valid
var diy = {
	phone: function(phone) {
		var reg = /^0{0,1}(13[0-9]|15[7-9]|153|156|17[0-9]|18[7-9])[0-9]{8}$/;
		return reg.test(phone);
	},
	IsLegalXmlChar: function(character) {
		return (
			character == 0x9 /* == '\t' == 9   */ ||
			character == 0xA /* == '\n' == 10  */ ||
			character == 0xD /* == '\r' == 13  */ ||
			(character >= 0x20 && character <= 0xD7FF) ||
			(character >= 0xE000 && character <= 0xFFFD) ||
			(character >= 0x10000 && character <= 0x10FFFF)
		);
	},
	IsLegalXmlString: function(str) {
		var isOk = true;
		for (var i = 0; i < str.length; i++) {
			if (!diy.IsLegalXmlChar(str.charCodeAt(i))) {
				isOk = false;
				break;
			}
		}
		return isOk;
	},
	nohtml: function(value) {
		var htmlLabel = /<|>/;
		return htmlLabel.test(value);
	},
	str: function(value) {
		var reg = /^[a-zA-Z]|[\u4E00-\u9FA5]+$/;
		return reg.test(value);
	}
};



var msg = {
	tips: function(msg, eleID) {
		layer.tips(msg, eleID, {
			tips: [2, '#de0f18']
		});
	}
};


function Order() {
	this.count = $('#orderlist .price').length;
}


// Object.prototype.mg = function(value) {
// 	debugger
// 	// alert(this.selector);
// 	if(value==0)
// 		return false;
// 	if (this != undefined) {
// 		msg.tips(value,this.selector);
// 	}
// }